package com.jie.eclipse.pan.editors;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorSite;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.xml.core.internal.provisional.contenttype.ContentTypeIdForXML;

import com.jie.eclipse.pan.editors.editor.JSPEditor;
import com.jie.eclipse.pan.editors.form.BackPage;
import com.jie.eclipse.pan.editors.form.PllPage;
import com.jie.eclipse.pan.editors.form.UIPage;
import com.jie.eclipse.pan.utils.Utils;
import com.jie.eclipse.pan.xml.XMLNode;


/**
 * An example showing how to create a multi-page editor.
 * This example has 3 pages:
 * <ul>
 * <li>page 0 contains a nested text editor.
 * <li>page 1 allows you to change the font used in page 2
 * <li>page 2 shows the words in page 0 in sorted order
 * </ul>
 */
public class PLLFormEditor extends BaseFormEditor{
	private PllPage pllPage = null;
	private UIPage uiPage = null;
	private BackPage backPage = null;
	
	private StructuredTextEditor pllEditor = null ;
	private JSPEditor uiEditor = null;
	private JSPEditor backEditor = null;
	private JSPEditor oldEditor = null;
	
	

	@Override
	protected IEditorSite createSite(IEditorPart page) {
		IEditorSite site = null;
		if (page == pllEditor) {
			site = new MultiPageEditorSite(this, page) {
				public String getId() {
					// Sets this ID so nested editor is configured for XML source
					return ContentTypeIdForXML.ContentTypeID_XML + ".source"; //$NON-NLS-1$;
				}
			};
		}
		else {
			site = super.createSite(page);
		}
		return site;
	}
	
	public PLLFormEditor() {
		super();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
	}
	
	public void createPage0() {
		pllEditor = new StructuredTextEditor();
		try {
			int index = addPage(pllEditor, getEditorInput());
			setPageText(index, "配置源码");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	public void createPage01() {
		this.pllPage = new PllPage(this);
		try {
			addPage(this.pllPage);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Creates page 0 of the multi-page editor,
	 * which contains a text editor.
	 */
	public void createPage1() {
		uiEditor = new JSPEditor();
		try {
			int index = addPage(uiEditor, getUiInput());
			setPageText(index, "UI源码");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Creates page 1 of the multi-page editor,
	 * which allows you to change the font used in page 2.
	 */
	public void createPage2() {
		backEditor = new JSPEditor();
		try {
			int index = addPage(backEditor, getBackInput());
			setPageText(index, "BACK源码");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	public void createPage3() {
		if(getOldInput() == null) return;
		oldEditor = new JSPEditor();
		try {
			int index = addPage(oldEditor, getOldInput());
			setPageText(index, "Old Jsp 源码");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void addPages() {
		createPage01();
		createPage0();
		createPage1();
		createPage2();
		createPage3();
	}
	
	public void dispose() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		super.dispose();
	}
	/**
	 * Saves the multi-page editor's document.
	 */
	public void doSave(IProgressMonitor monitor) {
		if(this.getActivePageInstance() == this.pllPage || this.getActiveEditor() == this.pllEditor){
			this.pllEditor.doSave(monitor);
			
			TreeViewer treeViewer = this.pllPage.getBlock().getTreeViewer();
			if(this.getActiveEditor() == this.pllEditor){
				this.pllPage.reloadPageNode();
				treeViewer.setInput(new XMLNode[]{this.pllPage.getPageNode()});
				treeViewer.expandAll();
//				treeViewer.collapseAll();
			}else{
				treeViewer.refresh(true);
			}
			
			if(this.uiEditor.isDirty()){
				this.uiEditor.doSave(monitor);
			}
			
			if(this.backEditor.isDirty()){
				this.backEditor.doSave(monitor);
			}
		}else{
			this.getActiveEditor().doSave(monitor);
		}
	}
	/**
	 * Saves the multi-page editor's document as another file.
	 * Also updates the text for page 0's tab, and updates this multi-page editor's input
	 * to correspond to the nested editor's.
	 */
	public void doSaveAs() {
		IEditorPart editor = getEditor(0);
		editor.doSaveAs();
		setPageText(0, editor.getTitle());
		setInput(editor.getEditorInput());
	}
	/* (non-Javadoc)
	 * Method declared on IEditorPart
	 */
	public void gotoMarker(IMarker marker) {
		setActivePage(0);
		IDE.gotoMarker(getEditor(0), marker);
	}
	/**
	 * The <code>MultiPageEditorExample</code> implementation of this method
	 * checks that the input is an instance of <code>IFileEditorInput</code>.
	 */
	public void init(IEditorSite site, IEditorInput editorInput)
		throws PartInitException {
		if (!(editorInput instanceof IFileEditorInput))
			throw new PartInitException("Invalid Input: Must be IFileEditorInput");
		super.init(site, editorInput);
	}
	/* (non-Javadoc)
	 * Method declared on IEditorPart.
	 */
	public boolean isSaveAsAllowed() {
		return true;
	}
	
	public StructuredTextEditor getPllEditor() {
		return pllEditor;
	}

	public JSPEditor getUiEditor() {
		return uiEditor;
	}

	public JSPEditor getBackEditor() {
		return backEditor;
	}
	
	public JSPEditor getOldEditor() {
		return oldEditor;
	}
	
	public PllPage getPllPage() {
		return pllPage;
	}
	
//	/**
//	 * Calculates the contents of page 2 when the it is activated.
//	 */
//	protected void pageChange(int newPageIndex) {
//		super.pageChange(newPageIndex);
//		if (newPageIndex == 2) {
//			sortWords();
//		}
//	}
	
//	/**
//	 * Closes all project files on project close.
//	 */
	public void resourceChanged(final IResourceChangeEvent event){
		if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
			Display.getDefault().asyncExec(new Runnable(){
				public void run(){
					IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
					Utils.print(event.getResource().toString());
					for (int i = 0; i<pages.length; i++){
//						if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){
//							IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput());
//							pages[i].closeEditor(editorPart,true);
//						}
					}
				}            
			});
		}
	}
//	/**
//	 * Sets the font related data to be applied to the text in page 2.
//	 */
//	void setFont() {
//		FontDialog fontDialog = new FontDialog(getSite().getShell());
//		fontDialog.setFontList(text.getFont().getFontData());
//		FontData fontData = fontDialog.open();
//		if (fontData != null) {
//			if (font != null)
//				font.dispose();
//			font = new Font(text.getDisplay(), fontData);
//			text.setFont(font);
//		}
//	}
//	/**
//	 * Sorts the words in page 0, and shows them in page 2.
//	 */
//	void sortWords() {
//
//		String editorText =
//			editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();
//
//		StringTokenizer tokenizer =
//			new StringTokenizer(editorText, " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\");
//		ArrayList editorWords = new ArrayList();
//		while (tokenizer.hasMoreTokens()) {
//			editorWords.add(tokenizer.nextToken());
//		}
//
//		Collections.sort(editorWords, Collator.getInstance());
//		StringWriter displayText = new StringWriter();
//		for (int i = 0; i < editorWords.size(); i++) {
//			displayText.write(((String) editorWords.get(i)));
//			displayText.write(System.getProperty("line.separator"));
//		}
//		text.setText(displayText.toString());
//	}
	
}
